package com.yy.controller;

import com.yy.domain.Category;
import com.yy.model.Result;
import com.yy.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("prod/category")
@Api(tags = "分类管理接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 全查询，不分页
     * 走缓存
     */
    @GetMapping("table")
    @ApiOperation("查询分类列表")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadCategorys(){
        List<Category> categoryList=this.categoryService.loadCategorys();
        return Result.success(categoryList);
    }

    @ApiOperation("查询所有可用父分类列表")
    @PreAuthorize("hasAuthority('prod:category:page')")
    @GetMapping("listCategory")
    public Result<List<Category>> loadParentCategorys(){
        return Result.success(this.categoryService.loadParentCategorys());
    }

    @PostMapping
    @ApiOperation("新增分类")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public Result<String> addCategory(@RequestBody @Validated Category category){

        Integer row=categoryService.addCategory(category);
        return Result.handle(row>0);
    }

}

